
let line = '5 2 3 2 4 3 5 2 1 4 3'
let arr = line.split(' ')
let root = new Node(arr[1])
let num = arr[0]
for (let i = 2; i < arr.length - 2; i += 2) {
    let tempNode1 = new Node(arr[i])
    let tempNode2 = root
    while (tempNode2 !== null) {
        if (tempNode2.value === arr[i + 1]) {
            let temp = tempNode2.next

            tempNode2.next = tempNode1
            tempNode1.next = temp
            break
        }
        tempNode2 = tempNode2.next
    }
}

let aim = arr[arr.length - 1]
let p = root
while (p.next != null) {
    if (+p.next.value === 3) {
        p.next = p.next.next
    }
    p = p.next
}

p = root
let res = []
while (p != null) {
    res.push(p.value)
    p = p.next
}

console.log(res.join(' '))

function Node(val) {
    this.value = val
    this.next = null

}